//
//  QXHomeViewModel.swift
//  DanTangSwift
//
//  Created by 众力金融 on 2017/6/21.
//  Copyright © 2017年 ..... All rights reserved.
//

import UIKit
import SwiftyJSON

class QXHomeViewModel: NSObject {
    
    lazy var homeItems = [QXHomeTopicModel]()
    
    func loadHomeData(homeId: Int, finished: @escaping (_ err: Error?) -> ()) {
        
        let url = BASE_URL + "v1/channels/\(homeId)/items"
        let params = ["gender": 1,
                      "generation": 1,
                      "limit": 20,
                      "offset": 0]
        NetWorkTool.requestData(type: .get, urlString: url, parameters: params, succeed: { (result) in

            let dict = JSON(result)
            let data = dict["data"].dictionary
            guard let items = data!["items"]?.arrayObject else {
                return
            }
            
            for item in items {
                
                let homeItem = QXHomeTopicModel(dict: item as! [String : Any])
                self.homeItems.append(homeItem)
            }
    
            finished(nil)
            
        }) { (err) in
            finished(err)
        }
    }
}
